home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / OBJECT.H < prev    next >
C/C++ Source or Header  |  1993-04-27  |  11KB  |  266 lines

  1. #ifndef OBJECT_H
  2. #define OBJECT_H
  3.  
  4. #include <iostream.h>
  5.  
  6. typedef int bool;
  7. const bool YES = 1;
  8. const bool NO = 0;
  9.  
  10. inline int           ABS(int x)             { return x >= 0 ? x : -x; }
  11. inline long          ABS(long x)            { return x >= 0 ? x : -x; }
  12. inline double        ABS(double x)          { return x >= 0 ? x : -x; }
  13. inline int           MAX(int a,int b)       { return a >= b ? a : b; }
  14. inline long          MAX(long a,long b)     { return a >= b ? a : b; }
  15. inline double        MAX(double a,double b) { return a >= b ? a : b; }
  16. inline unsigned int  MAX(unsigned int a, unsigned int b)
  17.                                             { return a >= b ? a : b; }
  18. inline unsigned long MAX(unsigned long a, unsigned long b)
  19.                                             { return a >= b ? a : b; }
  20. inline int           MIN(int a,int b)       { return a <= b ? a : b; }
  21. inline long          MIN(long a,long b)     { return a <= b ? a : b; }
  22. inline double        MIN(double a,double b) { return a <= b ? a : b; }
  23. inline unsigned int  MIN(unsigned int a, unsigned int b)
  24.                                             { return a <= b ? a : b; }
  25. inline unsigned long MIN(unsigned long a, unsigned long b)
  26.                                             { return a <= b ? a : b; }
  27.  
  28.  
  29. ////////////////////////////////////////////////////////////
  30. // macro DEFINE_CLASS
  31. ////////////////////////////////////////////////////////////
  32. #define DEFINE_CLASS(classname, basename)                    \
  33. const Class class_##classname = Class( class_##basename,     \
  34.                                        #classname,           \
  35.                                        sizeof(classname));   \
  36. const Class* classname::isA() const { return &class_##classname; }
  37.  
  38. class Class;        // forward reference for Object
  39. ////////////////////////////////////////////////////////////
  40. // class Object (declaration)
  41. ////////////////////////////////////////////////////////////
  42. class Object
  43. {
  44. protected:
  45.                 // protected constructor
  46.                 Object() {}
  47. public:
  48.     const Class*            baseClass() const;
  49.     const char*             className() const;
  50.  
  51.                             // unimplemented virtual function
  52.     void                    derivedClassResponsibility(const char*) const;
  53.  
  54.                             // invalid object class error
  55.     void                    invalidArgClass(const Object& ob,
  56.                                             const Class& expect,
  57.                                             const char* fname) const; 
  58.  
  59.                             // invalid object species error
  60.     void                    invalidArgSpecies(const Object& ob,
  61.                                               const Class& expect,
  62.                                               const char* fname) const;
  63.  
  64.                             // class cannot implement this function
  65.     void                    shouldNotImplement(const char*) const;
  66.  
  67.                             // validate argument class
  68.     void                    assertArgClass(const Object& ob,        
  69.                                            const Class& expect,
  70.                                            const char* fname) const; 
  71.  
  72.                             // validate argument species
  73.     void                    assertArgSpecies(const Object& ob,      
  74.                                              const Class& expect,
  75.                                              const char* fname) const;
  76.                                             
  77.     bool                    isKindOf(const Class&) const;           
  78.                             // YES if MemberOf class or a superclass
  79.  
  80.     bool                    isSame(const Object& ob)     const  { return this==&ob; }
  81.     bool                    isSpecies(const Class& clid) const 
  82.                             { 
  83.                                 return species()==&clid; 
  84.                             }
  85.  
  86.     virtual void            printOn(ostream& strm) const;
  87.     virtual unsigned        hash() const;           // calculate object hash
  88.     virtual const Class*    isA() const;            // return class descriptor address
  89.     virtual bool            isEqual(const Object&) const; // equality test
  90.     virtual int             compare(const Object&) const; // compare objects
  91.     virtual unsigned        capacity() const;       // subclass capacity
  92.     virtual Object*         copy() const;           // copy defaulted as deepCopy
  93.     virtual Object*         deepCopy() const;       // copy with distinct
  94.                                                     // instance variables
  95.     virtual void            deepenShallowCopy();    // convert shallow copy to deep copy
  96.     virtual Object*         shallowCopy() const;    // copy with shared
  97.                                                     // instance variables
  98.     virtual unsigned        size() const;           // # of objects in
  99.                                                     // array/container subclass
  100.     virtual const Class*    species() const;        // return species class
  101.                                                     // descriptor address
  102. };
  103.  
  104.  
  105.  
  106.  
  107. ////////////////////////////////////////////////////////////
  108. // class Class
  109. ////////////////////////////////////////////////////////////
  110. class Class : public Object
  111. {
  112.     const Class*    superClass;
  113.     Class*          nextClass;
  114.     const char*     class_name;
  115.     unsigned        inst_size;  // size of instance variables
  116. public:
  117.                     // constructor
  118.                     Class(const Class& super, const char* name, unsigned size);
  119.  
  120.     const Class*            baseClass() const               { return superClass; }
  121.     const char*             className() const               { return class_name; }
  122.     virtual void            printOn(ostream& strm) const;
  123.  
  124.     virtual bool            isEqual(const Object&) const;   // equality test
  125.     virtual unsigned        size() const;
  126.     virtual const Class*    isA() const;
  127. };
  128.  
  129.  
  130. extern const Class class_Class; // Class of Classes
  131. extern const Class class_Object;
  132.  
  133.  
  134. ////////////////////////////////////////////////////////////
  135. // Declare nil Object
  136. ////////////////////////////////////////////////////////////
  137. extern const Object* const nil; // pointer to sole instance of nil object  
  138.  
  139.  
  140.  
  141. ////////////////////////////////////////////////////////////
  142. // class Object (inline definitions)
  143. ////////////////////////////////////////////////////////////
  144. inline const Class*     Object::baseClass() const
  145.                         {
  146.                             return isA()->baseClass();
  147.                         }
  148.  
  149. inline const char*      Object::className() const
  150.                         {
  151.                             return isA()->className();
  152.                         }
  153.  
  154. ////////////////////////////////////////////////////////////
  155. // Global inline operator<<
  156. ////////////////////////////////////////////////////////////
  157. inline ostream&         operator<<(ostream& strm, const Object& ob)
  158.                         {
  159.                             ob.printOn(strm);
  160.                             return strm;
  161.                         }
  162.  
  163. ////////////////////////////////////////////////////////////
  164. // 4/27/93 -gmv
  165. // Added to handle printing of far pointers
  166. // in small memory model.  Not tested for
  167. // large memory model
  168. ////////////////////////////////////////////////////////////
  169. #define NEW031693
  170. #ifdef NEW031693
  171. inline ostream& operator<<(ostream& strm, const void* p)
  172. {
  173.     // strm <<"|***NEW031693***|";          // TEMP DIAGNOSTIC
  174.     long  oldflags = strm.flags();          // save cout format flags
  175.     strm.setf(ios::showbase);               // '0x' before hex numbers
  176.     strm << hex << (long) p;                // print pointer in hex
  177.     strm.flags(oldflags);                   // restore cout format flags
  178.     return strm;
  179. }
  180. inline ostream& operator<<(ostream& strm, void* p)
  181. {
  182.     strm << "CAST as const";
  183.     return operator<<(strm, (const void*) p);
  184. }
  185.  
  186. #endif
  187. #undef NEW031693
  188.  
  189.  
  190. ////////////////////////////////////////////////////////////
  191. // Global error handlers (declarations)
  192. ////////////////////////////////////////////////////////////
  193. void            DTerror(const char* s1, const char* s2);
  194.  
  195. extern void     invalidArgClass(const Object& ob,
  196.                                 const Class& expect,
  197.                                 const char* fname); 
  198.  
  199. extern void     invalidArgSpecies(const Object& ob,
  200.                                   const Class& expect,
  201.                                   const char* fname);
  202.  
  203. extern void     invalidClass(const Object& ob,
  204.                              const Class& expect);
  205.  
  206. extern void     invalidSpecies(const Object& ob,
  207.                                const Class& expect);
  208.  
  209.  
  210.  
  211. ////////////////////////////////////////////////////////////
  212. // Assertions (for testing Object types)
  213. ////////////////////////////////////////////////////////////
  214. inline void     assertArgClass(const Object& ob,
  215.                                const Class& expect,
  216.                                const char* fname)
  217.                 {
  218.                     if (!((ob).isKindOf(expect))) ::invalidArgClass(ob,expect,fname);
  219.                 }
  220.  
  221. inline void     assertArgSpecies(const Object& ob,
  222.                                  const Class& expect,
  223.                                  const char* fname) // const
  224.                 {
  225.                     if (!((ob).isSpecies(expect))) ::invalidArgSpecies(ob,expect,fname);
  226.                 }
  227.  
  228.  
  229. ////////////////////////////////////////////////////////////
  230. // class Object (inline definitions)
  231. ////////////////////////////////////////////////////////////
  232. inline void     Object::assertArgClass(const Object& ob,
  233.                                    const Class& expect,
  234.                                    const char* fname) const
  235.                 {
  236.                     if (!((ob).isKindOf(expect))) 
  237.                         this->invalidArgClass(ob,expect,fname);
  238.                 }
  239.  
  240. inline void     Object::assertArgSpecies(const Object& ob,
  241.                                      const Class& expect,
  242.                                      const char* fname) const
  243.                 {
  244.                     if (!((ob).isSpecies(expect)))
  245.                         this->invalidArgSpecies(ob,expect,fname);
  246.                 }
  247.  
  248. ////////////////////////////////////////////////////////////
  249. // Global inline functions (assertions)
  250. ////////////////////////////////////////////////////////////
  251. inline void     assertClass(const Object& ob, const Class& expect)
  252.                 {
  253.                     if (!((ob).isKindOf(expect))) 
  254.                         ::invalidClass(ob,expect);
  255.                 }
  256.  
  257. inline void     assertSpecies(const Object& ob, const Class& expect)
  258.                 {
  259.                     if (!((ob).isSpecies(expect))) 
  260.                         ::invalidSpecies(ob,expect);
  261.                 }
  262.  
  263. #endif
  264.  
  265.  
  266.